package view;

import trans.BaseTransBean;
import trans.DeptTransBean;
import trans.LoginInfoBean;
import utils.UtilsBean;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import com.opensymphony.xwork2.ActionSupport;

import data.DeptInfoBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeptTreeAction extends ActionSupport {
	private String id;
	private JSONArray deptTree;

	public JSONArray getDeptTree() {
		return deptTree;
	}

	public void setDeptTree(JSONArray deptTree) {
		this.deptTree = deptTree;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String execute(){
		List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
		DeptTransBean trans = new DeptTransBean(false);
		LoginInfoBean manager = (LoginInfoBean)UtilsBean.getLoginManager();
		if (manager == null){
		}
		else if (id == null){
			DeptInfoBean node = null;
			if (manager.getSupper()){
				if (manager.getDeptno()==null || manager.getDeptno().equals("")){
					node = trans.selectRoot();
				}
				else{
					node = trans.selectDept(manager.getDeptno());
				}
			}
			else{
				node = trans.selectDept(manager.getDeptno());
			}
			
			if (node != null){
				Map<String,Object> item = new HashMap<String,Object>();
				item.put("id", node.getDeptno());
				item.put("text", node.getDeptname()+"["+node.getDeptno()+"]");
				item.put("deptname", node.getDeptname());
				item.put("depttypename", node.getDepttypename());
				item.put("attributes", node.getDepttype());
				String deptaddr = node.getDeptaddr();
				if (deptaddr == null){
					deptaddr = "";
				}
				item.put("deptaddr", deptaddr);
				String telephone = node.getTelephone();
				if (telephone == null){
					telephone = "";
				}
				item.put("telephone", telephone);
				item.put("updeptno", node.getUpdetpno());
				if (node.getSubcount() > 0){
					item.put("state", "closed");
				}
				else{
					item.put("state", "open");
				}
				items.add(item);
			}
			else{
			}
		}
		else{
			List<DeptInfoBean> nodes = trans.selectChild(id);
			for (DeptInfoBean node: nodes){
				Map<String,Object> item = new HashMap<String,Object>();
				item.put("id", node.getDeptno());
				item.put("text", node.getDeptname()+"["+node.getDeptno()+"]");
				item.put("deptname", node.getDeptname());
				item.put("attributes", node.getDepttype());
				item.put("depttypename", node.getDepttypename());
				String deptaddr = node.getDeptaddr();
				if (deptaddr == null){
					deptaddr = "";
				}
				item.put("deptaddr", deptaddr);
				String telephone = node.getTelephone();
				if (telephone == null){
					telephone = "";
				}
				item.put("telephone", telephone);
				item.put("updeptno", node.getUpdetpno());
				if (node.getSubcount() > 0){
					item.put("state", "closed");
				}
				else{
					item.put("state", "open");
				}
				items.add(item);
			}
		}
		BaseTransBean.closeDaoSession();
		
		JsonConfig jsonConfig = new JsonConfig();
		String[] excludsField = {"callback"}; 
		jsonConfig.setExcludes(excludsField);
		deptTree = JSONArray.fromObject(items, jsonConfig);
		return SUCCESS;
	}	
}
